package com.example.young.peopleaccommodationms;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

import DB.OrdersBean;
import DB.FileStoreRegInfo;

public class Register extends AppCompatActivity implements View.OnClickListener{

    public Button register_btn;
    public Button reset_btn;
    public EditText register_name_edit;
    public EditText register_id_edit;
    public EditText register_starttime_edit;
    public EditText register_price_edit;
    public EditText register_roomid_edit;
    public EditText register_days_edit;
    public EditText register_prepay_edit;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        register_btn = (Button) findViewById(R.id.register_btnPage);
        reset_btn = (Button) findViewById(R.id.register_reset);
        register_name_edit = (EditText) findViewById(R.id.register_edit);
        register_id_edit = (EditText) findViewById(R.id.register_idedit);
        register_starttime_edit = (EditText) findViewById(R.id.register_starttimeedit);
        register_price_edit = (EditText) findViewById(R.id.register_priceedit);
        register_roomid_edit = (EditText) findViewById(R.id.register_roomidedit);
        register_days_edit = (EditText) findViewById(R.id.register_daysedit);
        register_prepay_edit = (EditText) findViewById(R.id.register_prepaymentsedit);


        register_btn.setOnClickListener(this);
        reset_btn.setOnClickListener(this);

        register_starttime_edit.setText(getformatDate());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //登记信息写入数据库,注意信息格式的正确性
            case R.id.register_btnPage:
                String name = register_name_edit.getText().toString();
                String id = register_id_edit.getText().toString();
                String starttime = register_starttime_edit.getText().toString();
                String roomid = register_roomid_edit.getText().toString();
                String price = register_price_edit.getText().toString();
                String days = register_days_edit.getText().toString();
                String prePay = register_prepay_edit.getText().toString();

//                String name = "bob";
//                String id = "110";
//                String starttime = "201604";
//                String roomid = "323";
//                String price = "1000";
//                String days = "2-day";
//                String prePay = "10000";
                OrdersBean orders = new OrdersBean(name,id,starttime,roomid,price,days,prePay);

//                register_name_edit.setText(name);
//                register_id_edit.setText(id);
//                register_starttime_edit.setText(starttime);
//                register_roomid_edit.setText(roomid);
//                register_price_edit.setText(price);
//                register_days_edit.setText(days);
//                register_prepay_edit.setText(prePay);

                if (FileStoreRegInfo.addNewOrdersToFile(orders)) {
                    Toast.makeText(this,"成功！已登记入住信息",Toast.LENGTH_LONG).show();
                    //生成订单信息不可写可读
                    Intent intent = new Intent(this,OrdersInfoEnsure.class);
                    intent.putExtra("orders",orders);
                    startActivity(intent);
                }
                else {
                    Toast.makeText(this,"失败！请确认输出入住信息",Toast.LENGTH_LONG).show();
                }

                break;
            case R.id.register_reset:

                break;
        }
    }

    public String getformatDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String time = format.format(date);

        return time;
    }
}